package com.kzd.sdk.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.kzd.sdk.callback.KZD_OnItemChildClickListener;
import com.kzd.sdk.callback.KZD_OnItemClickListener;
import com.kzd.sdk.entity.KZD_GameCoupon;
import com.kzd.sdk.ui.adapter.holder.GameCouponHolder;
import com.kzd.sdk.utils.AppUtils;

import java.util.ArrayList;
import java.util.List;


public class KZD_GameCouponAdapter extends RecyclerView.Adapter<GameCouponHolder> {

    private List<KZD_GameCoupon> data = new ArrayList<>();

    private final Context mContext;

    private KZD_OnItemClickListener mOnItemClickListener;
    private KZD_OnItemChildClickListener mOnItemChildClickListener;

    public KZD_GameCouponAdapter(Context context) {
        this.mContext = context;
    }

    @NonNull
    @Override
    public GameCouponHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(AppUtils.getLayoutId(mContext, "kzd_item_game_coupon"), parent, false);
        return new GameCouponHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull GameCouponHolder holder, int position) {
        KZD_GameCoupon itemData = get(position);
        holder.tvGameCouponPrice.setText(itemData.getJian());
        holder.tvGameCouponPriceFull.setText(itemData.getMoney().equals("0") ? "任意金额" : "满" + itemData.getMoney() + "元可用");
        holder.tvGameCouponName.setText(itemData.getName());
        holder.tvGameCouponDesc.setText(new StringBuilder().append("仅限《").append(itemData.getGame_name()).append("》充值使用"));
        holder.tvGameCouponTime.setText(itemData.getEnd_str());
        if (itemData.getIs_draw()) {
            holder.ivGameCouponStatusBg.setImageResource(AppUtils.getDrawableId(mContext, "kzd_wlq_bg"));
            holder.tvGameCouponName.setTextColor(Color.parseColor("#000000"));
            holder.tvGameCouponDraw.setTextColor(Color.parseColor("#f45444"));
            holder.tvGameCouponDraw.setText("立\n即\n领\n取");
        } else {
            holder.ivGameCouponStatusBg.setImageResource(AppUtils.getDrawableId(mContext, "kzd_kb_icon"));
            holder.tvGameCouponName.setTextColor(Color.parseColor("#999999"));
            holder.tvGameCouponDraw.setTextColor(Color.parseColor("#999999"));
            holder.tvGameCouponDraw.setText("已\n领\n取");
        }
        holder.tvGameCouponDraw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemChildClickListener != null && itemData.getIs_draw()) {
                    mOnItemChildClickListener.onClick(v, position);
                }
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onClick(v, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public void setNewInstance(List<KZD_GameCoupon> gameCouponList) {
        if (gameCouponList == null) {
            this.data = new ArrayList<>();
        } else {
            this.data = gameCouponList;
        }
        notifyDataSetChanged();
    }

    public KZD_GameCoupon get(int position) {
        return data.get(position);
    }

    public void setOnItemClickListener(KZD_OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setOnItemChildClickListener(KZD_OnItemChildClickListener onItemChildClickListener) {
        this.mOnItemChildClickListener = onItemChildClickListener;
    }
}
